package com.foo.JunitTestCases;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Assert;
import org.junit.Test;

import com.foo.LoadingData.DataLoader;

public class LoadFileTest {

	private DataLoader  objLoadFile = new DataLoader(); 
	@Test
	public void testStoreData() {
		try
		{
			String fileSavePath = "";
			String filePath = fileSavePath;
			int actualFiles = 0,expectedFiles = 0;
			File file = new File(filePath);
			
			String localFilePath = fileSavePath + "sp500hst.txt";
			if(new File(localFilePath).exists())
			{
				File fileContent = new File(localFilePath);
				InputStream inputStream = new FileInputStream(fileContent);
				long length = fileContent.length();
				
			 if (length > Integer.MAX_VALUE) {
			        // File is too large
			    }
			   
			    byte[] bytes = new byte[(int)length];

			    int offset = 0,numRead = 0;
			    while (offset < bytes.length
			           && (numRead=inputStream.read(bytes, offset, bytes.length-offset)) >= 0) {
			        offset += numRead;
			    }

			    if (offset < bytes.length) {
			        throw new IOException("Could not completely read file "+file.getName());
			    }

			    inputStream.close();

			
				objLoadFile.storeData(bytes, 0, bytes.length);
				expectedFiles = 1;
				
				File actualfile = new File(filePath);
				if(actualfile.isDirectory())
				{
					String str[] = actualfile.list();
					
					for(int i =0;i<str.length;i++)
					{
						File f=new File(filePath + str[i]);
						if(f.isFile())
						{
							if(f.getName().equalsIgnoreCase("20090821-20100820.zip"))
							{
								actualFiles = actualFiles + 1;
							}
						} 
					}
				}
				
				Assert.assertEquals(expectedFiles, actualFiles);	
			}
			else
			{
				System.out.println("File Does not exists!!!!");
			}
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

}
